package com.cms.admin.action.report;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.PageBean;
import com.cms.admin.model.RegisterOrderExt;
import com.cms.admin.model.RegisterOrderReport;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.service.order.RegisterOrderService;
import com.cms.admin.util.Constants;
import com.cms.admin.util.DateUtils;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 余冲
 * @author YC
 *
 */
public class OrderReportAction0717 extends BaseAction implements ModelDriven<RegisterOrderExt>{

    /**
	 * 
	 */
	private static final long serialVersionUID = -1833874757943918109L;
	private TemplateService templateService;
    private RegisterOrderService registerOrderService;
    private RegisterOrderExt search=new RegisterOrderExt();
    private PageBean page;
    
    public String queryOrderReport()
    {
    	Date now = DateUtils.getDatetime();
    	
    	search.setOrderCreateTimeStart(DateUtils.formatDate(DateUtils.getDateAfter(now, -7)));
    	search.setOrderCreateTimeEnd(DateUtils.formatDate(now));
        return SUCCESS;
    }
    
    public String queryOrderReportStuff()
    {
    	Date now = DateUtils.getDatetime();
    	
    	search.setOrderCreateTimeStart(DateUtils.formatDate(DateUtils.getDateAfter(now, -7)));
    	search.setOrderCreateTimeEnd(DateUtils.formatDate(now));
        return SUCCESS;
    }
    
    public String ajaxQueryOrderReport()
    {
    		
    	int currentPage = search.getCurrentPage();
    	if (0==currentPage) {
    		currentPage=1;
		}
    	page = new PageBean(currentPage, search.getPageSize());	
    	search.setOrderCreateTimeStart(search.getOrderCreateTimeStart()+" 00:00:00");
    	search.setOrderCreateTimeEnd(search.getOrderCreateTimeEnd()+" 23:59:59");
    	List<RegisterOrderReport> list = registerOrderService.selectOrderForReport(search);
    	
    	
    	List<RegisterOrderReport> listnew =  new ArrayList<RegisterOrderReport>();
    	
    	RegisterOrderReport total  = new RegisterOrderReport();
    	total.setRegisterDate("合计");
    	page.setAllRow(list.size());
    	
    	for (int i = 0; i < list.size(); i++) {
    		RegisterOrderReport temp = list.get(i);
    		RegisterOrderReport newTemp  = new RegisterOrderReport();
    		newTemp.setRegisterCount(temp.getRegisterCount()*5);
    		newTemp.setRegisterTotal(temp.getRegisterTotal().multiply(new BigDecimal(5)));
    		newTemp.setRegisterPayoffTotal(temp.getRegisterPayoffTotal().multiply(new BigDecimal(5)));
    		newTemp.setRegisterDate(temp.getRegisterDate());
    		listnew.add(newTemp);
    		
    		total.setRegisterCount(total.getRegisterCount() +newTemp.getRegisterCount());
    		total.setRegisterTotal(total.getRegisterTotal().add(newTemp.getRegisterTotal()));
    		total.setRegisterPayoffTotal(total.getRegisterPayoffTotal().add(newTemp.getRegisterPayoffTotal()));
    	}
    	
    	listnew.add(0, total);
        String res = null;

        try {
            res = this.getQueryResult(listnew, Constants.TEMPLATE_REPORT_1_QUERY_RESULT, page);

            JSONObject o = new JSONObject();

            o.put("content", res);
            o.put("pageCount", page.getTotalPage());

            return ajaxJsonSuccessMessage(o);
        }catch (Exception e)
        {
            LOG.error("ajaxQueryStuff",e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }

    
    /**
     * 需要添加分页
     * @return
     */
    public String ajaxQueryOrderReportStuff()
    {
    	int currentPage = search.getCurrentPage();
    	if (0==currentPage) {
    		currentPage=1;
		}
    	page = new PageBean(currentPage, search.getPageSize());	
    	search.setOrderCreateTimeStart(search.getOrderCreateTimeStart()+" 00:00:00");
    	search.setOrderCreateTimeEnd(search.getOrderCreateTimeEnd()+" 23:59:59");
    	search.setStuffNo(search.getStuffNo());
    	search.setStuffName(search.getStuffName());
    	
    	List<RegisterOrderReport> list = registerOrderService.selectOrderForReportWithStuff(page,search);
    	
     	RegisterOrderReport total  = new RegisterOrderReport();
    	total.setAccepterName("全 部");
    	total.setAccepterid(Long.valueOf(0));
    	
    	if (StringUtils.isBlank(search.getStuffNo())&&StringUtils.isBlank(search.getStuffName())&&currentPage==1) {
        	List<RegisterOrderReport> listAll = registerOrderService.selectOrderForReportWithStuffAll(search);
        	for (int i = 0; i < listAll.size(); i++) {
        		RegisterOrderReport temp = listAll.get(i);
        		total.setRegisterCount(total.getRegisterCount() +temp.getRegisterCount());
        		total.setRegisterTotal(total.getRegisterTotal().add(temp.getRegisterTotal()));
        		total.setRegisterPayoffTotal(total.getRegisterPayoffTotal().add(temp.getRegisterPayoffTotal()));
        	}
        	list.add(0, total);
		}
    	
        String res = null;
      
        try {
            res = this.getQueryResult(list,Constants.TEMPLATE_REPORT_2_QUERY_RESULT,page);
            JSONObject o = new JSONObject();
            o.put("content", res);
            o.put("pageCount", page.getTotalPage());

            return ajaxJsonSuccessMessage(o);
        }catch (Exception e)
        {
            LOG.error("ajaxQueryStuff",e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }

    
    
    public String ajaxorderReportStuffDailyType()
    {
    	
    	search = getSearch();
    	search.setOrderCreateTimeStart(search.getOrderCreateTimeStart()+" 00:00:00");
    	search.setOrderCreateTimeEnd(search.getOrderCreateTimeEnd()+" 23:59:59");
    	List<RegisterOrderReport> list = new ArrayList<RegisterOrderReport>();
    	
    	
    	Long accepterid = search.getAccepterid();
    	if (accepterid==null||accepterid.intValue()==0) {
    		//查询总额
    		 list = registerOrderService.selectOrderForReportWithTypeId(search);
		}else{
			//查询单人
			 list = registerOrderService.selectOrderForReportWithStuffDailyTypeId(search);
		}
    	RegisterOrderReport total  = new RegisterOrderReport();
    	total.setRegisterDate("合计");
    	total.setRegistertypeid(0);
    		//全表查询才显示合计
    		//遍历合计
    	List<RegisterOrderReport> listnew =new ArrayList<RegisterOrderReport>();
    	
    	//registerOrder.registertypeid
        	for (int i = 0; i < list.size(); i++) {
        		
        		RegisterOrderReport temp = list.get(i);
        		
        		
        		RegisterOrderReport newTemp  = new RegisterOrderReport();
        		if (temp.getRegistertypeid()==4) {
        		
            		newTemp.setRegisterCount(temp.getRegisterCount()*9);
            		newTemp.setRegisterTotal(temp.getRegisterTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterPayoffTotal(temp.getRegisterPayoffTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterDate(temp.getRegisterDate());
            		newTemp.setRegistertypeid(temp.getRegistertypeid());
            		
        		}else{
					newTemp.setRegisterCount(temp.getRegisterCount()*5);
            		newTemp.setRegisterTotal(temp.getRegisterTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterPayoffTotal(temp.getRegisterPayoffTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterDate(temp.getRegisterDate());
            		newTemp.setRegistertypeid(temp.getRegistertypeid());
        		}
        		
        		total.setRegisterCount(total.getRegisterCount() +newTemp.getRegisterCount());
        		total.setRegisterTotal(total.getRegisterTotal().add(newTemp.getRegisterTotal()));
        		total.setRegisterPayoffTotal(total.getRegisterPayoffTotal().add(newTemp.getRegisterPayoffTotal()));
        		listnew.add(newTemp);
        	}
    	listnew.add(0, total);
    	
    	String res = null;
         try {
        	 
        	 HashMap map = new HashMap();
             map.put("registerOrderList", listnew);
             map.put("accepterName", search.getAccepterName());
             map.put("jo", JSONArray.fromObject( list ).toString());
             res = templateService.generateContentByFreeMarker(Constants.TEMPLATE_REPORT_2_QUERY_DETAIL_TYPE, map);

             JSONObject o = new JSONObject();
             o.put("content", res);

             return ajaxJsonSuccessMessage(o);
         }catch (Exception e)
         {
             LOG.error("ajaxQueryStuff",e);
             return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
         }
    }
    
    public String ajaxOrderReportStuffDailyChannel()
    {
    	
    	search = getSearch();
    	search.setOrderCreateTimeStart(search.getOrderCreateTimeStart()+" 00:00:00");
    	search.setOrderCreateTimeEnd(search.getOrderCreateTimeEnd()+" 23:59:59");
    	List<RegisterOrderReport> list = new ArrayList<RegisterOrderReport>();
    	
    	Long accepterid = search.getAccepterid();
    	if (accepterid==null||accepterid.intValue()==0) {
    		//查询总额
    		 list = registerOrderService.selectOrderForReportWithChannel(search);
		}else{
			//查询单人
			 list = registerOrderService.selectOrderForReportWithStuffDailyChannel(search);
		}
    	RegisterOrderReport total  = new RegisterOrderReport();
    	List<RegisterOrderReport> listnew =new ArrayList<RegisterOrderReport>();
    	total.setRegisterDate("合计");
    	total.setRegisterguideid("0");
    		//全表查询才显示合计
    		//遍历合计
        	for (int i = 0; i < list.size(); i++) {
        		RegisterOrderReport temp = list.get(i);
        		
        		RegisterOrderReport newTemp  = new RegisterOrderReport();
        		if (temp.getRegisterguideid()=="1") {
            		
            		newTemp.setRegisterCount(temp.getRegisterCount()*9);
            		newTemp.setRegisterTotal(temp.getRegisterTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterPayoffTotal(temp.getRegisterPayoffTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterDate(temp.getRegisterDate());
            		newTemp.setRegisterguideid(temp.getRegisterguideid());
        		}else if (temp.getRegisterguideid()=="4") {
            		
            		newTemp.setRegisterCount(temp.getRegisterCount()*12);
            		newTemp.setRegisterTotal(temp.getRegisterTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterPayoffTotal(temp.getRegisterPayoffTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterDate(temp.getRegisterDate());
            		newTemp.setRegisterguideid(temp.getRegisterguideid());
        		}else if (temp.getRegisterguideid()=="7") {
            		
            		newTemp.setRegisterCount(temp.getRegisterCount()*9);
            		newTemp.setRegisterTotal(temp.getRegisterTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterPayoffTotal(temp.getRegisterPayoffTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterDate(temp.getRegisterDate());
            		newTemp.setRegisterguideid(temp.getRegisterguideid());
        		}
        		else{
					newTemp.setRegisterCount(temp.getRegisterCount()*5);
            		newTemp.setRegisterTotal(temp.getRegisterTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterPayoffTotal(temp.getRegisterPayoffTotal().multiply(new BigDecimal(5)));
            		newTemp.setRegisterDate(temp.getRegisterDate());
            		newTemp.setRegisterguideid(temp.getRegisterguideid());
        		}
        		
        		total.setRegisterCount(total.getRegisterCount() +newTemp.getRegisterCount());
        		total.setRegisterTotal(total.getRegisterTotal().add(newTemp.getRegisterTotal()));
        		total.setRegisterPayoffTotal(total.getRegisterPayoffTotal().add(newTemp.getRegisterPayoffTotal()));
        		listnew.add(newTemp);
        		
        	}
        	listnew.add(0, total);
    	
    	String res = null;
         try {
        	 
        	 HashMap map = new HashMap();
             map.put("registerOrderList", listnew);
             map.put("accepterName", search.getAccepterName());
             map.put("jo", JSONArray.fromObject( list ).toString());
             res = templateService.generateContentByFreeMarker(Constants.TEMPLATE_REPORT_2_QUERY_DETAIL_CHANNEL, map);

             JSONObject o = new JSONObject();
             o.put("content", res);

             return ajaxJsonSuccessMessage(o);
         }catch (Exception e)
         {
             LOG.error("ajaxQueryStuff",e);
             return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
         }
    }
    

    private String getQueryResult(List<RegisterOrderReport> list,String freeMarkerName,PageBean page)
            throws Exception {
        JSONArray jo = JSONArray.fromObject( list );//
        HashMap map = new HashMap();
        map.put("registerOrderList", list);
        map.put("page", page);
        map.put("jo", jo.toString());
        
        String result = "";
        result = templateService.generateContentByFreeMarker(freeMarkerName, map);
        return result;
        
        
    }
    
    
    


    public RegisterOrderService getRegisterOrderService() {
		return registerOrderService;
	}


	public void setRegisterOrderService(RegisterOrderService registerOrderService) {
		this.registerOrderService = registerOrderService;
	}


	public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }


	@Override
	public RegisterOrderExt getModel() {
		// TODO Auto-generated method stub
		return search;
	}


	public RegisterOrderExt getSearch() {
		return search;
	}


	public void setSearch(RegisterOrderExt search) {
		this.search = search;
	}

	public PageBean getPage() {
		return page;
	}

	public void setPage(PageBean page) {
		this.page = page;
	}
	
	
}
